import java.io.*;
import java.util.*;

public class Main {
	BufferedReader bf;
	StringTokenizer st;
	StringBuilder sb;
	
	int n,
		HH,
		MM,
		from,
		to;
	int[] diff = {-6, -5, -4, 0, 8, 9};
	String[] country = {"USW", "USC", "USE", "UK", "TW", "JA"};
	
	public Main() {
		bf = new BufferedReader(new InputStreamReader(System.in));
		sb = new StringBuilder();
	}
	
	public static void main(String[] args) {
		Main main = new Main();
		main.beginProcess();
	}

	public void beginProcess() {
		try {
			String input;
			while((input = bf.readLine()) != null) {
				n = Integer.parseInt(input);
				while(n > 0) {
					st = new StringTokenizer(bf.readLine());
					setInit(st.nextToken(), st.nextToken(), st.nextToken());
					
					transToUK();
					getTimeDiff();
					
					n--;
				}
			}			
			System.out.print(sb);
		}
		catch(Exception ex) {
			System.err.println(ex.toString());
		}
	}
	
	private void setInit(String curTime, String from, String to) {
		this.HH = Integer.parseInt(curTime) / 100;
		this.MM = Integer.parseInt(curTime) % 100;
		this.from = determineCountry(from);
		this.to = determineCountry(to);
	}
	
	private int determineCountry(String country) {
		int i = 3;
		if(country.equals("USW")) i = 0;
		else if(country.equals("USC")) i = 1;
		else if(country.equals("USE")) i = 2;
		else if(country.equals("TW")) i = 4;
		else if(country.equals("JA")) i = 5;
		return i;
	}
	
	private void transToUK() {		
		if(from != 3) {
			HH -= diff[from];
			if(HH >= 24) HH %= 24;
		}
	}
	
	private void getTimeDiff() {
		String paddingHH = "",
			   paddingMM = "";
		if(to != 3) {
			HH += diff[to];
			if(HH >= 24) HH %= 24;
		}
		
		if(HH == 0) paddingHH = "00";
		else if(HH / 10 == 0) paddingHH = "0" + HH;
		else paddingHH = Integer.toString(HH);
		if(MM == 0) paddingMM = "00";
		
		else if(MM / 10 == 0) paddingMM = "0" + MM;
		else paddingMM = Integer.toString(MM);
		
		sb.append(paddingHH + paddingMM + " " + country[to] + "\n");
	}
	
}